package es.ats.codington.spring.controllers;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.services.VisitorService;

/**
 * User Controller
 *
 */
@Controller
public class UsersController{  
	
	/**
	 * This controller receives a name and a password and checks if the visitor exists.
	 * If visitor exists a session is created and it redirects to "vieweventsofuser" controller.
	 * If visitor does not exist the page is updated with an error alert.
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/login.htm")
	public String loginController(HttpServletRequest request, HttpServletResponse response) throws IOException {	
		
		Visitor u =new Visitor();
		u.setVisitorName(request.getParameter("username"));  
		u.setPassword(request.getParameter("password"));  
	    
	    VisitorService vs = new VisitorService();
	    
	    try {

	    	if(vs.login(u)){
	    		
	    		u.setIdVisitor(vs.getIdVisitor(u));
	    		u=vs.getVisitor(u);
	    		System.out.println(u.getIsAdmin());
	    		request.getSession().setAttribute("visitor", u);
	    		
	    		return "redirect:/vieweventsofuser.htm";
	    	}
	    	else{  
	    		request.setAttribute("error", "Incorrect Login");

	    		return "NewCodington_visitorLogin"; 		
	    	} 
	    }
	    catch(Exception eee){
	    	return eee.getMessage();
	    }
	}
	

	/**
	 * This controller sends to the homepage (Login page), closing the session of visitor.
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/logout.htm")
	public String logoutController(HttpServletRequest request, HttpServletResponse response) {
		HttpSession sesion = request.getSession(true);
		sesion.invalidate();
		return "NewCodington_visitorLogin";
	}


	/**
	 * This controller sends to the registration page.
	 * @return
	 */
	@RequestMapping(value = "/VisitorRegistrationPage.htm")
	public String registerController() {
		return "NewCodington_visitorRegistration";
	}
	
	/**
	 * This controller sends to the homepage (Login page).
	 * @return
	 */
	@RequestMapping(value = "/LoginPage.htm")
	public String loginPageController() {
		return "NewCodington_visitorLogin";
	}

	/**
	 * This controller receives all implemented parameters in the registration form.
	 * A session is created and it redirects to "vieweventsofuser" controller.
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/VisitorRegistration.htm")
	public String validaterRegisterController(HttpServletRequest request, HttpServletResponse response) throws IOException {

			Visitor u =new Visitor();
		    
		    u.setFirstName(request.getParameter("firstname"));
		    u.setLastName(request.getParameter("lastname"));
		    u.setEmail(request.getParameter("emailAddress"));
		    u.setPhone(	Long.parseLong(request.getParameter("phone"))	);
		    u.setDni(request.getParameter("dni"));
		    u.setAddress(request.getParameter("addres"));
		    u.setVisitorName(request.getParameter("username"));
		    u.setPassword(request.getParameter("password"));   

		    VisitorService vs = new VisitorService();	
		    
		    try {
		    	if(vs.registerNewVisitor(u)){
		    		
		    		u.setIdVisitor(vs.getIdVisitor(u));
		    		u=vs.getVisitor(u);
		    		request.getSession().setAttribute("visitor", u);
		    		
		    		return "redirect:/vieweventsofuser.htm";
		    			
		    	}
		    	else{  
		    		request.setAttribute("error", "User NOT registered");
		    		
		    		return "NewCodington_visitorRegistration";	
		    	} 
		    }
		    catch(Exception eee){
		    	return eee.getMessage();
		    }	    
	}
	

	/**
	 * This controller sends to the page where the visitor is updated.
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	@RequestMapping(value = "UpdateVisitorPage.htm")
	public String updateVisitorPageController(HttpServletRequest request, HttpServletResponse response) throws SQLException, ClassNotFoundException {

		return "NewCodington_visitorUpdate";
	}
	
	/**
	 * This controller sends to the page where the password is updated.
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	@RequestMapping(value = "UpdatePasswordPage.htm")
	public String updatePasswordPageController(HttpServletRequest request, HttpServletResponse response) throws SQLException, ClassNotFoundException {

		return "NewCodington_updatePassword";
	}
	
	/**
	 * This controller receives all implemented parameters in the form of update page
	 * and updates the visitor.
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	@RequestMapping(value = "UpdateVisitor.htm")
	public String updateVisitorController(HttpServletRequest request, HttpServletResponse response) throws SQLException, ClassNotFoundException {
		
		HttpSession session=request.getSession();
		Visitor u = (Visitor) session.getAttribute("visitor");
	   
	    
		
	    VisitorService vs = new VisitorService();
	    
	    try
	    {
	    	
	    	if(u.getPassword().equals(request.getParameter("password"))){
	    		
	    		u.setFirstName(request.getParameter("firstname"));
	    	    u.setLastName(request.getParameter("lastname"));
	    	    u.setEmail(request.getParameter("emailAddress"));
	    	    u.setPhone(	Long.parseLong(request.getParameter("phone"))	);
	    	    u.setDni(request.getParameter("dni"));
	    	    u.setAddress(request.getParameter("addres"));
	    	    u.setVisitorName(request.getParameter("username"));
	    		
	    		if(vs.updateVisitor(u))
	    		{
	    			
	    			u=vs.getVisitor(u);
		    		request.getSession().setAttribute("visitor", u);
		    		request.setAttribute("error", "Visitor updated");
	    			return "NewCodington_visitorUpdate";  	
	    		}
	    		
	    		else{
	    			request.setAttribute("error", "NOT updated");
	    		return "NewCodington_visitorUpdate";  
	    		}
	    	}
	    	else
	    	{
	    		request.setAttribute("error", "Incorrect password");
	    		return "NewCodington_visitorUpdate";
	    	}
 	
	    }
	    catch(Exception eee){
	    	return eee.getMessage();
	    }

	}
	
	/**
	 * This controller receives all implemented parameters in the form of update password page
	 * and updates the visitor password.
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	@RequestMapping(value = "UpdatePassword.htm")
	public String updatePasswordController(HttpServletRequest request, HttpServletResponse response) throws SQLException, ClassNotFoundException {
		
		HttpSession session=request.getSession();
		Visitor u = (Visitor) session.getAttribute("visitor");
	    
	    VisitorService vs = new VisitorService();
	    
	    try
	    {
	    	
	    	if(request.getParameter("oldpassword").equals(u.getPassword())){
	    		
	    		u.setPassword(request.getParameter("password"));
	    			
	    		if(vs.updateVisitor(u))
	    		{
	    			
	    			u=vs.getVisitor(u);
		    		request.getSession().setAttribute("visitor", u);
		    		request.setAttribute("error", "Password updated");
		    		return "NewCodington_updatePassword";	
	    		}
	    		else{
	    			request.setAttribute("error", "Not updated");
	    			return "NewCodington_updatePassword";
	    		}
	    	}
	    	else
	    	{
	    		request.setAttribute("error", "Incorrect password");
	    		 
	    		return "NewCodington_updatePassword";
	    	}
	    		
	    }
	    catch(Exception eee){
	    	return eee.getMessage();	
	    }	
	}

}
